#include "projectHeader.h"





void* userTerminal(void*)
{
	
action.sa_handler = handlerForCntrlC;
sigaction(SIGINT, &action, NULL);
pthread_sigmask(SIG_UNBLOCK, &set, NULL);

	

	while(1)
	{
		pthread_mutex_lock(&cntrlCFlagMutex);
		cntrlCFlag=false;	
		statusRespMap.clear();
		//lastStatusType=0;
		pthread_mutex_unlock(&cntrlCFlagMutex);
		memset(userInput,'\0',512);
		fflush(stdin);
		
		printf("servant:%hu>\n",port);
		
		//scanf("%s",userInput);
		gets(userInput);
		convertCase(userInput);
		if(strncmp(userInput,"shutdown",strlen("shutdown"))==0)
		{
			initiateUserShutDownSequence();
			//printf("USER TERMINAL:Shutdown command detected \n");
		}
		else if(strncmp(userInput,"status",strlen("status"))==0)
		{
			allowGet=false;
//			printf("USER TERMINAL:Status command detected on terminal.\n");
			
//			printf("USER INPUT is %s\n",userInput);
			char *temp=NULL;
			temp=strtok(userInput," ");

//			printf("~~~~~~~~~~~~~~~~~~~~~~TEMP is %s\n",temp);
			char *temp2=strtok(NULL," ");
			if(temp2==NULL)
			{
			printf("Invalid values.\n");
			continue;
			}
//			printf("!!!!!!!!!!!!!!!!!!!!!!!!!TEMP2 is %s\n",temp2);
			memset(&statusFileName[0],'\0',512);
			char *temp3=strtok(NULL," ");
			statusInputTTL=atoi(temp3);
			if(statusInputTTL>255)
			{
				printf("TTL cannot be more than 255 as header contains only 1byte for ttl msg.\n");
				continue;
			}
//			printf("TTL from status command: %u\n",statusInputTTL);
			char *temp4=strtok(NULL," ");
//			printf("\nhere...1...\n");
			memcpy(&statusFileName[0],&(temp4[0]),strlen(temp4));
//			printf("Status File Name: %s\n",statusFileName);

			if(strncmp(temp2,"neighbors",strlen("neighbors"))==0)
			{

//				printf("Status Command for neighbors.\n");
			
			//			printf("\nhere...2...\n");
						if(statusInputTTL!=0)
						{
							//write to nam file.

							lastStatusType=1;
							//flood the status msg to all the connected neighbours.
							//Generate a queueItem with status req and push it to th eevent queue.
							char type=0x01; //Will change in proj 2
			//				printf("\nhere...3...\n");
							statusReqMsg statusData(type);
			//				printf("\nhere...4...\n");
							queueItem statusReqToBePushed;
			//				printf("\nhere...5...\n");
							statusReqToBePushed.sockfd=-1;
			//				printf("\nhere...6...\n");
							statusReqToBePushed.msgHeader.messageType=0xAC;
			//				printf("\nhere...7...\n");
							unsigned char* buff1=(unsigned char*)malloc(20*sizeof(unsigned char));
			//				printf("\nhere...8...\n");
							char* msg = (char*)"STRQ";
			//				printf("\nhere...9...\n");
			//				printf("\n*****************************(((((((((((((((((((%s\n",nodeInstanceId);
			//				printf("\nhere...10...\n");
							buff1=GetUOID((char *)nodeInstanceId,msg,buf, sizeof(buf));
							memcpy(&(statusReqToBePushed.msgHeader.UOID[0]),&buf[0],20);
							
							statusReqToBePushed.msgHeader.ttlInHeader=statusInputTTL;
							statusReqToBePushed.msgHeader.msgDataLength=1;

							memcpy(&(statusReqToBePushed.dataBuf[0]),&(statusData.statusType),statusReqToBePushed.msgHeader.msgDataLength);


							pthread_mutex_lock(&eventQueueMutex);

								eventQueue.push_back(statusReqToBePushed);
								//printf("USER TERMINAL:STATUS REQ MSG Pushed to Event Dispatcher.\n");
								pthread_cond_broadcast(&eventQueueCV);


							pthread_mutex_unlock(&eventQueueMutex);
							
							///Code to write into a NAM file 

							








							//TODO:Think of NAM file to signal here so that terminal is only displayed after NAM file is written.


						}

						writeToNAMFile(statusInputTTL);


						//printf("Status File Name: %s\n",statusFileName);

			}
			else if(strncmp(temp2,"files",strlen("files"))==0)
			{	
//				printf("Status command for files.\n");
				if(statusInputTTL!=0)
						{
							//write to nam file.
							lastStatusType=2;

							//flood the status msg to all the connected neighbours.
							//Generate a queueItem with status req and push it to th eevent queue.
							char type=0x02; //Will change in proj 2
			//				printf("\nhere...3...\n");
							statusReqMsg statusData(type);
			//				printf("\nhere...4...\n");
							queueItem statusReqToBePushed;
			//				printf("\nhere...5...\n");
							statusReqToBePushed.sockfd=-1;
			//				printf("\nhere...6...\n");
							statusReqToBePushed.msgHeader.messageType=0xAC;
			//				printf("\nhere...7...\n");
							unsigned char* buff1=(unsigned char*)malloc(20*sizeof(unsigned char));
			//				printf("\nhere...8...\n");
							char* msg = (char*)"STRQ";
			//				printf("\nhere...9...\n");
			//				printf("\n*****************************(((((((((((((((((((%s\n",nodeInstanceId);
			//				printf("\nhere...10...\n");
							buff1=GetUOID((char *)nodeInstanceId,msg,buf, sizeof(buf));
							memcpy(&(statusReqToBePushed.msgHeader.UOID[0]),&buf[0],20);
							
							statusReqToBePushed.msgHeader.ttlInHeader=statusInputTTL;
							statusReqToBePushed.msgHeader.msgDataLength=1;

							memcpy(&(statusReqToBePushed.dataBuf[0]),&(statusData.statusType),statusReqToBePushed.msgHeader.msgDataLength);

							
							pthread_mutex_lock(&eventQueueMutex);

								eventQueue.push_back(statusReqToBePushed);
								//printf("USER TERMINAL:STATUS REQ MSG Pushed to Event Dispatcher.\n");
								pthread_cond_broadcast(&eventQueueCV);


							pthread_mutex_unlock(&eventQueueMutex);
							
							
							msgLifetimer();

	//						printf("Status for files is complete.\n");








							//TODO:Think of NAM file to signal here so that terminal is only displayed after NAM file is written.


						}
				

				//Code for new status files command.
				// USE the statusFileName and statusInputTTL varibles for ttl and file name.
			
			}
			else
			{
				printf("Status Command does not support anything else except \"files\" and \"neighbors\".\n");
			}











		}
		else if(strncmp(userInput,"store",strlen("store"))==0)
		{
			allowGet=false;
	//		printf("Store Command Detected..\n");
	//		printf("USER INPUT is %s\n",userInput);
			char *temp=NULL;
			temp=strtok(userInput," ");

	//		printf("~~~~~~~~~~~~~~~~~~~~~~TEMP is %s\n",temp);
			char *temp2=strtok(NULL," ");
			if(temp2==NULL)
			{
			printf("Invalid values.\n");
			continue;
			}
	//		printf("!!!!!!!!!!!!!!!!!!!!!!!!!TEMP2 is %s\n",temp2);
			memset(&storeFileName[0],'\0',512);
			memcpy(&storeFileName[0],&(temp2[0]),strlen(temp2));
			char *temp3=strtok(NULL," ");
			if(temp3!=NULL)
			{
				storeInputTTL=atoi(temp3);
			}
			else
			{
				printf("Invalid Commandline.\n");
				continue;
			}

			if(storeInputTTL>255)
			{
				//printf("TTL cannot be more than 255 as header contains only 1byte for ttl msg.\n");
				printf("Invalid Commandline.\n");
				continue;
			}
	//		printf("TTL from Store command: %u\n",storeInputTTL);
			char a[512];
			memset(&a[0],'\0',512);
			bool termFlag=false;
			bool invalidKeywordFlag=false;
			char *temp5=strtok(NULL,"\0");
			int z=0;
			int znew=0;
			
			if(temp5!=NULL)		
			{
					//printf("TEMP5 is %s\n",temp5);

					char b[512];
					memset(&b[0],'\0',512);
					memcpy(&b[0],&temp5[0],strlen(temp5));
					//printf("TEMP5 is %s\n",b);
					do
					{	
						char temp4[512];
						memset(&temp4[0],'\0',512);
						/*if(z==0)
						{
						temp4=strtok(temp5,"\" ");
						z++;
						}
						else
						{
							temp4=strtok(NULL,"\" ");
						}
						*/
						z=znew;
						//bool end=false;
						while(b[z]!='\0')
						{
							if(b[z+1]=='\0')
							{
								termFlag=true;
								z++;
								break;
							}
							if(b[z]=='\"' && b[z+1] == ' ')
							{
								z++;
								break;
							}
							z++;
						}
						

						//if(!end)
						//{
							memcpy(&temp4[0],&b[znew],z-znew);
						
						//}
						znew=z+1;
						if(temp4[0]!='\0')
						{

							//printf("TagValuePair is: %s\n",temp4);
							memcpy(&a[0],&(temp4[0]),strlen(temp4));
							
							char tagPair[512];
							char valuePair[512];

							memset(&tagPair[0],'\0',512);
							memset(&valuePair[0],'\0',512);
							int i=0;
							while(a[i]!='=')
							{
								if(a[i]=='\0')
								{
									printf("Invalid Commandline.\n");
									invalidKeywordFlag = true;
									termFlag = true;

									break;
								}

								i++;
							}
							if(invalidKeywordFlag == true)
								break;
							memcpy(&tagPair[0],&a[0],i);
							//cout<<a[i]<<endl;
							if(a[i+1]!='\0')
							{
								memcpy(&valuePair[0],&a[i+2],(strlen(temp4)-i-3));
							}
							else
							{
								printf("Invalid Commandline.\n");
								termFlag = true;
								invalidKeywordFlag = true;
								continue;
							}
							//printf("Tag Name is :%s\n",tagPair);
							storeKeywordsList.push_front(string(tagPair));
							//printf("Value Name is :%s\n",valuePair);
							
							
							char *temp6=strtok(valuePair," ");
							//	printf("Split is %s\n",temp6);
							
							while(temp6!=NULL)
							{
								char tempvaluepair[512];
								memset(&tempvaluepair[0],'\0',512);
								memcpy(&tempvaluepair[0],&temp6[0],strlen(temp6));
								storeKeywordsList.push_front(string(tempvaluepair));
								temp6=strtok(NULL," ");
							//	if(temp6!=NULL)
							//	printf("Split is %s\n",temp6);
							}


							//storeKeywordsList.push_front(string(valuePair));

							
							//Save the values somewhere to be used later.

						}
						else
						{
							termFlag=true;
						}
					}
					while (!termFlag);
					if(invalidKeywordFlag)
						continue;

			}
			processStoreCmd();
			storeKeywordsList.clear();
//			printf("Store command Completed.. !!!!!!!!!! Reset All variables and keywords list.!!!!!!!!!!!!!.\n");


		}
		else if(strncmp(userInput,"search",strlen("search"))==0)
		{
			searchIndex = 1;
			indexSearchResponseMap.clear();
			allowGet=true;
			bool termFlag=false;
	//		printf("Search Command Detected..\n");
			char *temp=NULL;
			temp=strtok(userInput," ");

		//	printf("~~~~~~~~~~~~~~~~~~~~~~TEMP is %s\n",temp);
			//char temp2[512];
			//memset(&temp2[0],'\0',512);

			char *temp2=strtok(NULL,"\0");
			if(temp2==NULL)
			{
				printf("Invalid values.\n");
				allowGet=false;
				continue;
			}
			/*
			int k=0;
			while(userInput[k] != ' ')
			{
				
				printf("userinput[k] is %x K is: %d\n",userInput[k],k);
				k++;
			}
			printf("userinput[k] is %x K is: %d\n",userInput[k],k);
			memcpy(&temp2[0],&userInput[k+1],strlen(userInput)-k-1);
			*/
		//	printf("!!!!!!!!!!!!!!!!!!!!!!!!!TEMP2 is %s\n",temp2);

			char searchType[512];
			char searchValue[512];
			char a[512];
			memset(&a[0],'\0',512);
			memcpy(&a[0],&(temp2[0]),strlen(temp2));
					
			//char tagPair[512];
			//char valuePair[512];

			memset(&searchType[0],'\0',512);
			memset(&searchValue[0],'\0',512);
			int i=0;
			while(a[i]!='=')
			{
				if(a[i]=='\0')
				{
					printf("Invalid Commandline.\n");
				//	invalidKeywordFlag = true;
					termFlag = true;
					allowGet=false;

					break;
				//	continue;
				}
				i++;
			}
			if(termFlag)
				continue;
			memcpy(&searchType[0],&a[0],i);
			//cout<<a[i]<<endl;
			//memcpy(&searchValue[0],&a[i+1],(strlen(temp2)-i-1));
			//printf("SearchType is: %s\n",searchType);
			//printf("SearchType is: %s\n",searchValue);
			
			/*	search message (MessageType=0xEC)
				create search packet
				fill header
				fill data
				BytePos		Name			Description
				0			MessageType		The message type. Please refer to the next table.
				1-20		UOID			Message ID. This field is inserted by a message originating node and copied by all forwarding nodes.
				21			TTL				How many hops the message has left before it should be dropped.
				22			(Reserved)		(always zero)
				23-26		Data Length		The length of the function-dependent data.
				27			Search Type		If the search type is 1, the next field contains an exact file name. 
											If the search type is 2, the next field contains an exact SHA1 hash value (not hexstring-encoded).
											If the search type is 3, the next field contains a list of keywords (separated by space characters).
				28+			Query			The content of this field depends on the search type.

			*/

			if(strncmp(searchType,"filename",strlen("filename"))==0)
			{
				memcpy(&searchValue[0],&a[i+1],(strlen(temp2)-i-1));
		//		printf("SearchType is: %s\n",searchValue);
				if(searchValue[0] == '\0')
				{
					allowGet=false;
					printf("Invalid Commandline. Enter filename.\n");
					continue;
				}
				char type=0x01;		//searchType = 1;
				queueItem searchReqToBePushed;
				searchReqToBePushed.sockfd=-1;
				//	printf("\nhere...6...\n");
				searchReqToBePushed.msgHeader.messageType=0xEC;
				unsigned char* buff1=(unsigned char*)malloc(20*sizeof(unsigned char));
				//	printf("\nhere...8...\n");
				char* msg = (char*)"SHRQ";
				//	printf("\nhere...9...\n");
				//	printf("\n*****************************(((((((((((((((((((%s\n",nodeInstanceId);
				//	printf("\nhere...10...\n");
				buff1=GetUOID((char *)nodeInstanceId,msg,buf, sizeof(buf));
				memcpy(&(searchReqToBePushed.msgHeader.UOID[0]),&buf[0],20);
							
				searchReqToBePushed.msgHeader.ttlInHeader=ttl;
				searchReqToBePushed.msgHeader.msgDataLength=strlen(searchValue)+1;

				//Query = searchValue;
				memcpy(&(searchReqToBePushed.dataBuf[0]),&(type),1);
				memcpy(&(searchReqToBePushed.dataBuf[1]),&searchValue[0],(searchReqToBePushed.msgHeader.msgDataLength-1));

					// Write code to push into eventdispatcher queue.		
					pthread_mutex_lock(&eventQueueMutex);

						eventQueue.push_back(searchReqToBePushed);
						//printf("USER TERMINAL:STATUS REQ MSG Pushed to Event Dispatcher.\n");
						pthread_cond_broadcast(&eventQueueCV);


					pthread_mutex_unlock(&eventQueueMutex);

				
			}
			else if(strncmp(searchType,"sha1hash",strlen("sha1hash"))==0)
			{
				memcpy(&searchValue[0],&a[i+1],(strlen(temp2)-i-1));	
			//	printf("SearchType is: %s\n",searchValue);
				if(searchValue[0] == '\0')
				{
					allowGet=false;
					printf("Invalid Commandline. Enter filename.\n");
					continue;
				}
				/*if(strlen(searchValue)!=40)
				{
					allowGet=false;
					printf("Invalid Commandline. Enter filename.\n");
					continue;
				}*/

				char type=0x02;		//searchType = 2;
				queueItem searchReqToBePushed;
				searchReqToBePushed.sockfd=-1;
				//	printf("\nhere...6...\n");
				searchReqToBePushed.msgHeader.messageType=0xEC;
				unsigned char* buff1=(unsigned char*)malloc(20*sizeof(unsigned char));
				//	printf("\nhere...8...\n");
				char* msg = (char*)"SHRQ";
				//	printf("\nhere...9...\n");
				//	printf("\n*****************************(((((((((((((((((((%s\n",nodeInstanceId);
				//	printf("\nhere...10...\n");
				buff1=GetUOID((char *)nodeInstanceId,msg,buf, sizeof(buf));
				memcpy(&(searchReqToBePushed.msgHeader.UOID[0]),&buf[0],20);
							
				searchReqToBePushed.msgHeader.ttlInHeader=ttl;
				searchReqToBePushed.msgHeader.msgDataLength=strlen(searchValue)+1;

				//Query = searchValue;
				memcpy(&(searchReqToBePushed.dataBuf[0]),&(type),1);
				memcpy(&(searchReqToBePushed.dataBuf[1]),&searchValue[0],(searchReqToBePushed.msgHeader.msgDataLength-1));

					// Write code to push into eventdispatcher queue.		
					pthread_mutex_lock(&eventQueueMutex);

						eventQueue.push_back(searchReqToBePushed);
						//printf("USER TERMINAL:STATUS REQ MSG Pushed to Event Dispatcher.\n");
						pthread_cond_broadcast(&eventQueueCV);


					pthread_mutex_unlock(&eventQueueMutex);
			}
			else if(strncmp(searchType,"keywords",strlen("keywords"))==0)
			{
				if(a[i+1]=='"')
				{
					//printf("Quote Detected.%s\n",temp2);
					memcpy(&searchValue[0],&a[i+2],(strlen(temp2)-i-3));
					//printf("SearchType is: %s\n",searchValue);
				}
				else
				{
					//printf("Quote not Detected.%s\n",temp2);
					memcpy(&searchValue[0],&a[i+1],(strlen(temp2)-i-1));
					char* str=NULL;
					str=strtok(searchValue," ");

					char *temp2=strtok(NULL,"\0");
					//printf("%s\n",temp2);
					if(temp2!=NULL)
					{
						printf("Invalid values.\n");
						allowGet=false;
						continue;
					}
				//	printf("SearchType is: %s\n",searchValue);
				}
				if(searchValue[0] == '\0')
				{
					allowGet=false;
					printf("Invalid Commandline.\n");
					continue;
				}
				char type=0x03;		//searchType = 3;
				queueItem searchReqToBePushed;
				searchReqToBePushed.sockfd=-1;
				//	printf("\nhere...6...\n");
				searchReqToBePushed.msgHeader.messageType=0xEC;
				unsigned char* buff1=(unsigned char*)malloc(20*sizeof(unsigned char));
				//	printf("\nhere...8...\n");
				char* msg = (char*)"SHRQ";
				//	printf("\nhere...9...\n");
				//	printf("\n*****************************(((((((((((((((((((%s\n",nodeInstanceId);
				//	printf("\nhere...10...\n");
				buff1=GetUOID((char *)nodeInstanceId,msg,buf, sizeof(buf));
				memcpy(&(searchReqToBePushed.msgHeader.UOID[0]),&buf[0],20);
							
				searchReqToBePushed.msgHeader.ttlInHeader=ttl;
				searchReqToBePushed.msgHeader.msgDataLength=strlen(searchValue)+1;

				//Query = searchValue;
				memcpy(&(searchReqToBePushed.dataBuf[0]),&(type),1);
				memcpy(&(searchReqToBePushed.dataBuf[1]),&searchValue[0],(searchReqToBePushed.msgHeader.msgDataLength-1));

				// Write code to push into eventdispatcher queue.		
					pthread_mutex_lock(&eventQueueMutex);

						eventQueue.push_back(searchReqToBePushed);
						//printf("USER TERMINAL:STATUS REQ MSG Pushed to Event Dispatcher.\n");
						pthread_cond_broadcast(&eventQueueCV);


					pthread_mutex_unlock(&eventQueueMutex);

			}
			else
			{
				printf("Invalid command: Search can only have either \"filename\" or \"sha1hash\" or \"keywords\"\n");
				allowGet=false;
				continue;
			}
			
			msgLifetimer();



			// insert my details into UOID MAP.
	/*		sleep(15);
			pthread_mutex_lock(&indexMutex);
				printf("------------------------------------------------------\n");
				//printf("\nMap Size is : %d\n",indexSearchResponseMap.size());
				map<int,searchResponseInfo>::iterator displayIterator;
				for(displayIterator = indexSearchResponseMap.begin(); displayIterator != indexSearchResponseMap.end(); displayIterator++)
				{
					printf("SearchIndex = %d ---> File ID  is:\n",(*displayIterator).first);
					printUOID((*displayIterator).second.fileID);
				//	printf("Original File Name is : %s\n",(*displayIterator).second.originalFileName);
				//	cout<<"Am i origin: "<<(*displayIterator).second.isOrigin<<endl;
				//	printf("File Meta Buffer is : %s\n",(*displayIterator).second.fileMetaBuffer);
				}
				printf("------------------------------------------------------\n");
				pthread_mutex_unlock(&indexMutex);
				*/
	//			sleep(5);
				pthread_mutex_lock(&indexMutex);
				//printf("------------------------------------------------------\n");
				//printf("\nMap Size is : %d\n",indexSearchResponseMap.size());
				map<int,searchResponseInfo>::iterator displayIterator;
				for(displayIterator = indexSearchResponseMap.begin(); displayIterator != indexSearchResponseMap.end(); displayIterator++)
				{
					char *ptrTag[10];
					int tagId=0;
					char *ptrTagValue[10];
					int tagValueId=0;
					char tempMeta[8192];
					memset(&tempMeta[0],'\0',8192);
					memcpy(&tempMeta[0],&(*displayIterator).second.fileMetaBuffer[0],8192);
					for (int i = 0; i<7 ; i++)
					{
						if (i == 0)
						{
							ptrTag[tagId] = strtok((char*)tempMeta, "\n");
							
							//printf("MetaTag-%d : [%s]\n", tagId, ptrTag[tagId]);
							tagId++;
						}
						else
						{
							ptrTag[tagId] = strtok((char*)NULL, "=");
							
							//printf("MetaTag-%d : [%s]\n", tagId, ptrTag[tagId]);
							tagId++;
							ptrTagValue[tagValueId] = strtok((char*)NULL, "\n");
							
							//printf("MetaTagValue-%d : [%s]\n", tagValueId, ptrTagValue[tagValueId]);
							tagValueId++;
						}
					}
					printf("\n[%d]\tFileID=",(*displayIterator).first);
					for(int i=0; i <20 ; i++)
					{
						printf("%x", (*displayIterator).second.fileID[i]);
					}
					printf("\n");
					printf("\tFileName=%s\n",ptrTagValue[0]);
					//int filesize=0;
					//sscanf(ptrTagValue[1],"%d",filesize);
					printf("\tFileSize=%d\n",(int)atoi(ptrTagValue[1]));
					printf("\tSHA1=%s\n",ptrTagValue[2]);
					printf("\tNonce=%s\n",ptrTagValue[3]);
					printf("\tKeywords=%s\n",ptrTagValue[4]);

					/*
					FileID=02adefc1dfc97a082fa18a5ef1e8c487259b7fb4
					  FileName=foo
					  FileSize=123
					  SHA1=b83a758fecbefcd3ea547fbf0f9a97eba0ea984c
					  Nonce=01b7a1bd6f169dde22518a865ab2f44c70fcab82
					  Keywords=key1 key2 key3
					*/
				//	printf("Original File Name is : %s\n",(*displayIterator).second.originalFileName);
				//	cout<<"Am i origin: "<<(*displayIterator).second.isOrigin<<endl;
				//	printf("File Meta Buffer is : %s\n",(*displayIterator).second.fileMetaBuffer);
				}
				//printf("------------------------------------------------------\n");
				
				pthread_mutex_unlock(&indexMutex);


		}
		else if(strncmp(userInput,"get",strlen("get"))==0)
		{
			//allowGet=true;
			if(allowGet)
			{
//				printf("Get Command Detected..\n");
				
//				printf("USER INPUT is %s\n",userInput);
				char *temp=NULL;
				temp=strtok(userInput," ");
				
				char *temp2=strtok(NULL," ");
				if(temp2==NULL)
				{
					printf("Invalid number of arguments .\n");
					continue;
				}
				getFileNumber=0;
//				printf("!!!!!!!!!!!!!!!!!!!!!!!!!TEMP2 is %s\n",temp2);
				getFileNumber=atoi(temp2);
				char *temp3=strtok(NULL," ");
				//printf("!!!!!!!!!!!!!!!!!!!!!!!!!TEMP2 is %s\n",temp3);
				
				memset(&getFileName[0],'\0',512);
				if(temp3!=NULL)
				{
					getFileNameSpcified=true;
					
					memset(&getFileName[0],'\0',512);
					memcpy(&getFileName[0],&temp3[0],strlen(temp3));
//					printf("GetFileNumber is %d and GetFileName is %s\n",getFileNumber,getFileName);
					//continue;
					
				}
				else
				{
//					printf("GetFileNumber is %d\n",getFileNumber);
				}
				
				


//				printf("~~~~~~~~~~~~~~~~~~~~~~TEMP is %s\n",temp);
				if(getFileNumber<=(int)indexSearchResponseMap.size())
				{
					processGet();
					msgLifetimer();
				}
				else
				{
					printf("\nInvalid Get number. Please see search response.\n");
				}
//				printf("GET Command Over.\n");



			}
			else
			{
				printf("GET Not followed by get or search.\n");
			}
		}
		else if(strncmp(userInput,"delete",strlen("delete"))==0)
		{
			allowGet=false;
	//		printf("Delete Command Detected..\n");
			
	//		printf("USER INPUT is %s\n",userInput);
			char *temp=NULL;
			temp=strtok(userInput," ");

	//		printf("~~~~~~~~~~~~~~~~~~~~~~TEMP is %s\n",temp);
			char *temp2=strtok(NULL," ");
			if(temp2==NULL)
			{
				printf("Invalid number of arguments .\n");
				continue;
			}
		//	printf("!!!!!!!!!!!!!!!!!!!!!!!!!TEMP2 is %s\n",temp2);

			//printf("Length is %d",strlen(temp2));
			
			memset(&delFileName[0],'\0',512);
			if(strncmp(temp2,"filename=",strlen("filename="))==0)
			{
				memcpy(&delFileName[0],&temp2[strlen("filename=")],strlen(temp2)-strlen("filename="));
	//			printf("Filename to be deleted is: %s and strlen is %d\n",delFileName,(int)strlen(delFileName));
			}
			else
			{
				printf("Does not contain the Filename word. Usage \"FileName=xyz\"\n");
			}
			
			char *temp3=strtok(NULL," ");
			if(temp3==NULL)
			{
				printf("Invalid number of arguments .\n");
				continue;
			}
		//	printf("!!!!!!!!!!!!!!!!!!!!!!!!!TEMP3 is %s\n",temp3);
			
			
			memset(&delSHA1[0],'\0',41);
			if(strncmp(temp3,"sha1=",strlen("sha1="))==0)
			{
				memcpy(&delSHA1[0],&temp3[strlen("sha1=")],strlen(temp3)-strlen("sha1="));
				/*if((int)strlen(delSHA1)!=40)
				{
					printf("Invalid Sha1 length.\n");
					continue;
				}*/
		//		printf("SHA1 of file to be deleted is: %s and sha1 length is %d\n",delSHA1,(int)strlen(delSHA1));
			}
			else
			{
				printf("Does not contain the SHA1 word. Usage \"SHA1=xyz\"\n");
			}
			
			char *temp4=strtok(NULL," ");
			if(temp4==NULL)
			{
				printf("Invalid number of arguments .\n");
				continue;
			}
		//	printf("!!!!!!!!!!!!!!!!!!!!!!!!!TEMP4 is %s\n",temp4);
			
			memset(&delNonce[0],'\0',41);
			if(strncmp(temp4,"nonce=",strlen("nonce="))==0)
			{
				memcpy(&delNonce[0],&temp4[strlen("nonce=")],strlen(temp4)-strlen("nonce="));
				/*if((int)strlen(delNonce)!=40)
				{
					printf("Invalid Nonce length.\n");
					continue;
				}*/
		//		printf("Nonce of file to be deleted is: %s and nonce length is %d\n",delNonce,(int)strlen(delNonce));
			}
			else
			{
				printf("Does not contain the Nonce word. Usage \"Nonce=xyz\"\n");
			}
			
			processDelete();

		}
		else
		{
			printf("USER TERMINAL: INVALID Command.\n");
		}




	}
	pthread_exit(NULL);
}
